package  com.bill.app.serveur;


import java.io.*;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringTokenizer;

import com.bill.app.serveur.bean.Abonnes;
import com.bill.app.serveur.bean.dao.implement.MainDaoImplement;

 
public class IDChange extends Thread
{MainDaoImplement em;

    IDChange(Socket paquet, String mess)
    {
        this.paquet = null;
        this.mess = null;
        this.paquet = paquet;
        this.mess = mess;
        em=SERVEUR.getEm();
        start();
    }

    public void run()
    {
        String code = "";
        StringTokenizer tt = new StringTokenizer(mess, ":");
        tt.nextToken();
        code = tt.nextToken();
        if(code.compareToIgnoreCase("LP") == 0)
        {
            String ancienLogin = tt.nextToken();
            String nouveauLogin = tt.nextToken();
            String ancienpass = tt.nextToken();
            String nouveaupass = tt.nextToken();
            
                try {
					if(ThreadConnexion.existDansAbonne(ancienLogin, ancienpass) && !ThreadAjout.ajouterAmi(nouveauLogin,nouveauLogin))
					{
						Abonnes ab = em.loadAbonneByLogin(ancienLogin);
						ab.setLogin(nouveauLogin);
						ab.setPass(nouveaupass);
						
						em.updateLoginInAmiTable(ancienLogin, nouveauLogin);
						em.update(ab);
					}
					else
					    code = "FAILED";
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					code = "FAILED";
				}
             
             
        } else
        if(code.compareToIgnoreCase("L") == 0)
        {
            String ancienLogin = tt.nextToken();
            String nouveauLogin = tt.nextToken();
            try
            {
                if(ThreadAjout.ajouterAmi(ancienLogin,nouveauLogin) && !ThreadAjout.ajouterAmi(nouveauLogin,nouveauLogin))
                    try
                    {
                    	Abonnes ab = em.loadAbonneByLogin(ancienLogin);
                    	ab.setLogin(nouveauLogin);
                    	 
                    	
                    	em.updateLoginInAmiTable(ancienLogin, nouveauLogin);
                    	em.update(ab);  code = "SUCCESS_L";
                    }
                    catch(Throwable throwable) { }
                else
                    code = "FAILED";
            }
            catch(Throwable throwable1) { }
        } else
        if(code.compareToIgnoreCase("P") == 0)
        {
            String log = tt.nextToken();
            String ancienpass = tt.nextToken();
            String nouveaupass = tt.nextToken();
            
            Abonnes ab = em.loadAbonneByLogin(log);
            
        	 
        	
        	
        	 
        	
            if(em.isSamePassword(ab, ancienpass))
            {ab.setPass(nouveaupass);
            	em.update(ab);
                code = "SUCCESS_P";
            } else
            {
                code = "FAILED";
            }
        } else
        {
            code = "FAILED";
        }
        try
        {
            (new PrintWriter(new BufferedWriter(new OutputStreamWriter(paquet.getOutputStream())), true)).println(code);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    Socket paquet;
    String mess;
}
